/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.shsc.dao.implementations;

import com.bionic.shsc.dao.interfaces.ITestDAO;
import com.bionic.shsc.entities.AnswerData;
import com.bionic.shsc.entities.QuestionData;
import com.bionic.shsc.entities.TestData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ant
 */
public class MySQLTest extends QueryExecutor implements ITestDAO {

    @Override
    public List<TestData> getList() {
        try {
            ResultSet RS = query.getRSForQuery("select * from tst_test");
            List<TestData> res = new ArrayList<TestData>();
            while (RS.next()) {
                TestData tc = new TestData();
                tc.setTestID(RS.getInt("test_id"));
                tc.setTestName(RS.getString("test_name"));
                tc.setTestDescription(RS.getString("description"));
                res.add(tc);
            }
            return res;
        } catch (SQLException ex) {
            Logger.getLogger(MySQLUser.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    @Override
    public ArrayList<QuestionData> getTestQuestions(int testID) {
        String sqlText = "select * from shsc.tst_question where test_id = ? order by question_order ";
        try {
            PreparedStatement pstmt = query.getConnection().prepareStatement(sqlText);
            pstmt.setInt(1, testID);
            ResultSet RS = pstmt.executeQuery();
            ArrayList<QuestionData> res = new ArrayList<QuestionData>();
            while (RS.next()) {
                QuestionData question = new QuestionData();
                question.setTestID(RS.getInt("test_id"));
                question.setQuestionID(RS.getInt("question_id"));
                question.setParentAnswerID(RS.getInt("parent_answer_id"));
                question.setQuestionType(RS.getInt("question_type"));
                question.setText(RS.getString("text"));
                question.setMinAnswer(RS.getInt("min_answer"));
                // dict_question_type: type 2 - custom answers
                // type 0 - default one of three
                // type 1 - multiselect(problems)

                // default: bad, average, good
                if (question.getQuestionType() == QuestionData.QuestionTypeOneOfThree) {
                    question.setAnswerVariants(AnswerData.getDefaultAnswers(question.getQuestionID()));
                };
                // variants from db
                if (question.getQuestionType() == QuestionData.QuestionTypeOneAnswer) {
                    String answSqlText =
                            "SELECT `answer_variant`.`answer_id`,`answer_variant`.`question_id`, "
                            + "    `answer_variant`.`answer_order`, "
                            + "    `answer_variant`.`text`, "
                            + "    `answer_variant`.`weight` "
                            + "FROM `shsc`.`answer_variant` "
                            + "where `answer_variant`.`question_id` = ? "
                            + "order by `answer_order`";
                    PreparedStatement answ_pstmt = query.getConnection().prepareStatement(answSqlText);
                    answ_pstmt.setInt(1, question.getQuestionID());
                    ResultSet answ_RS = answ_pstmt.executeQuery();
                    ArrayList<AnswerData> answers = new ArrayList<AnswerData>();
                    while (answ_RS.next()) {
                        AnswerData answer = new AnswerData();
                        answer.setAnswerID(answ_RS.getInt("answer_id"));
                        answer.setAnswerID(answ_RS.getInt("question_id"));
                        answer.setAnswerOrder(answ_RS.getInt("answer_order"));
                        answer.setText(answ_RS.getString("text"));
                        answer.setWeight(answ_RS.getInt("weight"));
                        answers.add(answer);
                    }
                    question.setAnswerVariants(answers);
                }
                // question: multiselect problems
                // TODO: Multiselect questions
                res.add(question);
            }
            return res;
        } catch (SQLException ex) {
            Logger.getLogger(MySQLUser.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

   /* @Override
    public List<AnswerData> getTestAnswers(int testID) {
        String sqlText = "select * from answer_variant where answer_variant.question_id in (select question_id from tst_question"
                + "where tst_question.test_id = ? order by tst_question.question_order) order by answer_order";
        try {
            PreparedStatement pstmt = query.getConnection().prepareStatement(sqlText);
            pstmt.setInt(1, testID);
            ResultSet RS = pstmt.executeQuery();
            List<AnswerData> res = new ArrayList<AnswerData>();
            while (RS.next()) {
                AnswerData answer = new AnswerData();
                answer.setAnswerID(RS.getInt("answer_id"));
                answer.setQuestionID(RS.getInt("question_id"));
                answer.setAnswerOrder(RS.getInt("answer_order"));
                answer.setText(RS.getString("text"));
                res.add(answer);
            }
            return res;
        } catch (SQLException ex) {
            Logger.getLogger(MySQLUser.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    */
}
